<?php
/**
 * Created by PhpStorm.
 * User: 王飞
 * Date: 2017/9/9
 * Time: 23:37
 */
namespace app\index\controller;
use think\Controller;
use app\common\controller\WebFront;
use fast\third\Wechat;
use app\common\model\Userwechat;
use app\common\model\User;
use think\Exception;
use think\Session;


class Staff extends Controller
{
	protected $model = null;
	public function _initialize()
	{
		parent::_initialize();
	}
	public function  index(){
		/**
		 * 此页面为展示页面
		 * 加载页面需要的数据资源，并加载到页面中
		 */
		$sort = $this->request->get("sort", "id");
		$order = $this->request->get("order", "DESC");
		$search = $this->request->get("search", '');
		$type=$this->request->get("type", "1");
		
		$searchdata="s.type=".$type;
		$agedata=[0,200];
		if(isset($_GET['serverday'])) $searchdata.=" and s.serverdays_id".$_GET['serverday']." ";
		if(isset($_GET['grade'])) $searchdata.=" and s.grade_id=".$_GET['serverday']." ";
		if(isset($_GET['provice'])) $searchdata.=" and p.name=".$_GET['provice']." ";
		if(isset($_GET['age'])) $agedata="s.age=".explode($_GET['age'])." ";
		if(isset($_GET['animalyear'])) $searchdata.=" and a.name=".$_GET['animalyear']." ";
		if($sort=="comment") $sort="count(c.id)";
		else $sort='s.'.$sort;
		
		$data=db('staff')->alias('s')
			->join('grade g','s.grade_id=g.id')
			->join('province p','s.province_id=p.id')
			->join('animalsyear a','s.animalsyear_id=a.id')
			->order($sort,$order)
			->where('s.age','between',$agedata)
			->where($searchdata)
			->where("s.name",'like',"%".$search."%")
			->field('s.id id,s.avatar imgurl,p.name province,s.age age,s.serverdays_id day,s.price price,s.name name,g.name star')
			->select();
		foreach ($data as $key=>$value){
			$data[$key]['comnum']=db('comment')->where('staff_id',$value['id'])->count();
		}
			$result = array("success" => '1', "data" => $data);
			return json($result);
	}
	
	public function details(){
		if(isset($_GET['id'])){
			$status=1;
			$id=$_GET['id'];
			$data['info']=db('staff')
				->alias('s')
				->where('s.id',$id)
				->join('province p','p.id=s.province_id')
				->field('s.id id,s.name name,s.avatar imgurl,s.age age,p.name province,s.price price,s.serverdays_id day,s.grade_id star,s.tag info,s.grade_info backinfo,s.expert_comment expertcom')->find();
			$data['info']['comnum']=db('comment')->where('staff_id',$data['info']['id'])->count();
			$data['card']=db('staff_card')
				->alias('s')
				->join('card c','s.card_id=c.id')
				->where('s.staff_id',$id)
				->field('s.pic_src picurl,c.name picname')
				->select();
			$data['motherpic']=db('staff_pic')->where('pic_type_id',2)->where('staff_id',$id)->select();
			$data['time']=db('worklog')->where('staff_id',$id)->field('begintime,endtime,w_type')->select();
			
			$data['comment_rank']=db('comment')->alias('co')
				->join('comment_rank_value va','co.id=va.comment_id')
				->join('comment_ranks_type ty','ty.id=va.comment_rank_type_id')
				->where('co.staff_id',$id)
				->group('ty.name')
				->field('ty.name nam,avg(va.value) value')->select();
			$data['comment']=db('comment')->alias('c')
			//	->join('comment_pic p','c.id=p.comment_id')
				->where('c.staff_id',$id)
				->field('id,context,avatar imgurl,username,createtime')
				->select();
			foreach ($data['comment'] as $key=>$value){
				$data['comment'][$key]['picurl']=db('comment_pic')->alias('p')->where('p.comment_id='.$value['id'])->select();
			}
			$result = array("success" =>$status, "data" => $data);
			return json($result);
		}
		else{
			$status=-1;
			$msg="没有选中员工！";
			$result = array("status" =>$status, "msg" => $msg);
			return json($result);
		}
	}
}